package com.presto.directo.mvvm.view

import android.content.Intent
import android.os.Bundle
import android.view.*
import androidx.core.view.*
import com.presto.directo.R
import com.presto.directo.base.BaseFragment
import com.presto.directo.databinding.FragmentMineBinding
import com.presto.directo.net.*
import com.presto.directo.utils.*

class MineFragment : BaseFragment() {

    private val binding by lazy { FragmentMineBinding.inflate(layoutInflater) }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return binding.root
    }

    override fun initEvent() {
        binding.llLogin.setOnClickListener {
            mActivity.loginWith()
        }
        binding.ivSetting.setOnClickListener {
            mActivity.loginWith(false) {
                val intent = Intent(mActivity, SettingActivity::class.java)
                startActivity(intent)
            }
        }
        binding.rlCard.setOnClickListener {
            mActivity.loginWith {
                val intent = Intent(mActivity, CardListActivity::class.java)
                intent.putExtra("showIndicator", false)
                startActivity(intent)
            }
        }
        binding.rlRecord.setOnClickListener {
            mActivity.loginWith {
                val intent = Intent(mActivity, RecordListActivity::class.java)
                startActivity(intent)
            }
        }
        binding.rlQuestion.setOnClickListener {
            val intent = Intent(mActivity, WebActivity::class.java)
            intent.putExtra("url", ApiService.URL_QUESTION)
            intent.putExtra("title", getString(R.string.text_mine_question))
            startActivity(intent)
        }
        start(onNext = {
            val config = getApiService().getConfig(getHttpMap().sign(2)).getResult()
            binding.rlFacebook.isVisible = config.let { !it?.email.isNullOrBlank() || !it?.hot_tel.isNullOrBlank() || !it.contact_url.isNullOrBlank() }
            binding.rlFacebook.setOnClickListener {
                if (config.contact_url.isNullOrBlank()) {
                    val intent = Intent(mActivity, ContactActivity::class.java)
                    intent.putExtra("config", config)
                    startActivity(intent)
                } else {
                    val intent = Intent(requireActivity(), WebActivity::class.java)
                    intent.putExtra("url", config.contact_url)
                    intent.putExtra("title", getString(R.string.text_mine_contact))
                    startActivity(intent)
                }
            }
        }, toast = false)
    }

    override fun onResume() {
        super.onResume()
        if (UserUtil.isLogin()) {
            binding.tvPhone.text = UserUtil.getUser()?.phone ?: ""
            start(onNext = {
                val name = getApiService().getPersonInfo(getHttpMap().sign(22)).result?.real_name
                binding.tvName.isGone = name.isNullOrBlank()
                binding.tvName.text = name
            }, toast = false)
        } else {
            binding.tvPhone.text = getString(R.string.text_mine_login_tip)
            binding.tvName.text = ""
            binding.tvName.isGone = true
        }
    }

}